package com.test.wsdltest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void myClick(View v){
        switch(v.getId()){
            case R.id.btn_test:
                getCityList();
                break;
            case R.id.btn_test2:
                getWeather();
                break;
            case R.id.btn_test3:
                getWeather2();
                break;
            case R.id.btn_test4:
                getCityList2();
                break;
        }
    }



    public void getCityList(){
        String NAMESPACE = "http://tempuri.org/";
        // WebService地址
        String URL = "http://61.154.14.23:8765/webservice/ClinicAppointTest/ws12580Appoint.dll/wsdl/Iws12580Appoint";
        String METHOD_NAME = "getCityList";
        String SOAP_ACTION = NAMESPACE+METHOD_NAME;
        //加载wsdl
        HttpTransportSE ht = new HttpTransportSE(URL);
        ht.debug = true;

        // 使用SOAP1.1协议创建Envelop对象,SoapEnvelope.VER11,VER10,VER12//都不行
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        // 实例化SoapObject对象 NAMESPACE
        SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);

        // 添加请求参数
        soapObject.addProperty("authenticator ","12580");


        envelope.bodyOut = soapObject;

        // 设置与.Net提供的Web Service保持较好的兼容性，该webservice是delphi编写
        envelope.dotNet = true;
        envelope.setOutputSoapObject(soapObject);//设置请求参数

        try
        {
            // 调用Web Service
            Log.v("wsdl", "111");
            ht.call(SOAP_ACTION, envelope);
            Log.v("wsdl", "222");
            SoapObject result = (SoapObject) envelope.bodyIn;

            Log.v("wsdl", "333"+result.getProperty(0).toString());

        }
        catch (IOException e)
        {    Log.e("IO", "IO异常");
            e.printStackTrace();
        }
        catch (XmlPullParserException e)
        {
            Log.e("XmlPullParserException", "XmlPullParserException异常");
            e.printStackTrace();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void getWeather(){
        String NAMESPACE = "http://WebXml.com.cn/";
        String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
        String METHOD_NAME = "getWeatherbyCityName";
        String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
        SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
        //soapObject.addProperty("theCityCode", "0591");
        soapObject.addProperty("theCityName", "武汉");
        //soapObject.addProperty("authenticator ","1258");
        HttpTransportSE ht = new HttpTransportSE(URL);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.bodyOut = soapObject;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(soapObject);

        try {
            ht.call(SOAP_ACTION, envelope);
            Log.v("getWeather", "call back");
            SoapObject result = (SoapObject) envelope.bodyIn;


            Log.v("getWeather","result(0)---" + result.getProperty(0).toString());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //soapObject = (SoapObject) envelope.bodyIn;
        // SoapObject detail = (SoapObject) result.getProperty("getWeatherResult");
    }

    public void getWeather2(){
        String NAMESPACE = "http://WebXml.com.cn/";
        String reqUrl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
        String METHOD_NAME = "getWeatherbyCityName";
        String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
        //String soapRequest = "<?xml version=\"1.0\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"><SOAP-ENV:Body SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><NS1:getWeatherbyCityName xmlns:NS1=\"http://WebXml.com.cn/\"><theCityName xsi:type=\"xsd:string\">北京</theCityName></NS1:getWeatherbyCityName></SOAP-ENV:Body></SOAP-ENV:Envelope>";
        String soapRequest =
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                        "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"+
                        "<soap12:Body>"+
                        "<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"+
                        "      <theCityName>北京</theCityName>"+
                        "</getWeatherbyCityName>"+
                        "</soap12:Body>"+
                        "</soap12:Envelope>";
        //String soapRequest = "theCityName=北京";
        try{
            URL url =new URL(reqUrl);
            HttpURLConnection con=(HttpURLConnection)url.openConnection();
            byte[] bytes=soapRequest.getBytes();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            con.setRequestMethod("POST");
            con.setRequestProperty("SOAPAction",SOAP_ACTION);
            con.setRequestProperty("Content-Type", "application/soap+xml");
            con.setRequestProperty("Content-Length",""+bytes.length);
            OutputStream outStream=con.getOutputStream();
            outStream.write(soapRequest.getBytes());
            outStream.flush();
            //outStream.close();
            //con.connect();
            Log.v("aaaa", "bbbbbb");
            InputStream inStream=con.getInputStream();
            Log.v("aaaa", "bbbbbb");
            //BufferedReader br = new BufferedReader(new InputStreamReader(inStream),20000);
            String line = null;
            byte[] buf = new byte[1024];
            int nResult;
            while(true){
                nResult = inStream.read(buf);
                if(nResult==-1) break;
                Log.v("test", new String(buf));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void getCityList2(){
        String NAMESPACE = "urn:ws12580AppointIntf-Iws12580Appoint";
        String reqUrl = "http://61.154.14.23:8765/webservice/ClinicAppointTest/ws12580Appoint.dll/soap/Iws12580Appoint";
        //String reqUrl = "http://61.154.14.23:8765/webservice/ClinicAppointTest/ws12580Appoint.dll/wsdl/Iws12580Appoint";
        String METHOD_NAME = "GetCityList";
        String SOAP_ACTION = "urn:ws12580AppointIntf-Iws12580Appoint#GetCityList";
        //String soapRequest = "<?xml version=\"1.0\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"><SOAP-ENV:Body SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><NS1:GetCityList xmlns:NS1=\"urn:ws12580AppointIntf-Iws12580Appoint\"><authenticator xsi:type=\"xsd:string\">12580</authenticator></NS1:GetCityList></SOAP-ENV:Body></SOAP-ENV:Envelope>";
        String soapRequest = "<?xml version=\"1.0\"?><soap12:Envelope xmlns:soap12=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"><soap12:Body><NS1:GetCityList xmlns:NS1=\"urn:ws12580AppointIntf-Iws12580Appoint\"><authenticator xsi:type=\"xsd:string\">12580</authenticator></NS1:GetCityList></soap12:Body></soap12:Envelope>";

        try{
            URL url =new URL(reqUrl);
            HttpURLConnection con=(HttpURLConnection)url.openConnection();
            byte[] bytes=soapRequest.getBytes();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            con.setRequestMethod("POST");
            //con.setRequestProperty("SOAPAction",SOAP_ACTION);
            con.setRequestProperty("Content-Type", "application/soap+xml;charset=utf-8");
            con.setRequestProperty("Content-Length",""+bytes.length);
            OutputStream outStream=con.getOutputStream();
            outStream.write(bytes);
            outStream.flush();
            //outStream.close();
            //con.connect();
            Log.v("aaaa", "bbbbbb");
            InputStream inStream=con.getInputStream();
            Log.v("aaaa", "bbbbbb");
            //BufferedReader br = new BufferedReader(new InputStreamReader(inStream),20000);
            String line = null;
            byte[] buf = new byte[1024];
            int nResult;
            while(true){
                nResult = inStream.read(buf);
                if(nResult==-1) break;
                Log.v("test", new String(buf));
            }
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}
